package com.changgou.user.service.impl;

import com.changgou.entity.Result;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Spu;
import com.changgou.user.dao.CollectMapper;
import com.changgou.user.pojo.Collect;
import com.changgou.user.service.CollectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author Cloudy
 * @date 2020/5/29
 */
@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    private CollectMapper collectMapper;

    @Autowired
    private SpuFeign spuFeign;


    /**
     * Collect条件+分页查询
     * @param collect 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Collect> findPage(Collect collect, int page, int size) {
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(collect);
        //执行搜索
        return new PageInfo<Collect>(collectMapper.selectByExample(example));
    }

    /***
     * collect条件搜索方法
     * @param collect
     * @return
     */
    @Override
    public List<Collect> findList(Collect collect) {
        //构建查询条件
        Example example = createExample(collect);
        //根据构建的条件查询数据
        return collectMapper.selectByExample(example);
    }

    /***
     * 删除Collect
     * @param id
     */
    @Override
    public void delete(String id) {
        collectMapper.deleteByPrimaryKey(id);
    }

    /***
     * 新增Collect
     * @param collect
     */
    @Override
    public void add(Collect collect) {
        collectMapper.insert(collect);
    }

    /**
     * 根据ID查询Collect
     * @param id
     * @return
     */
    @Override
    public Collect findById(String id) {
        return collectMapper.selectByPrimaryKey(id);
    }

    /***
     * 查询所有Collect
     * @return
     */
    @Override
    public List<Collect> findAll() {
        return collectMapper.selectAll();
    }

    /***
     * 修改Collect数据
     * @param collect
     */
    @Override
    public void update(Collect collect) {
        collectMapper.updateByPrimaryKey(collect);
    }

    /***
     * Collect分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Collect> findPage(int page, int size) {
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Collect>(collectMapper.selectAll());
    }

    @Override
    public List<Spu> findByUsername(Collect collect) {
        //构建查询条件
        Example example = createExample(collect);
        //按主键升序排序
        example.setOrderByClause("id ASC");
        //根据构建的条件查询数据
        List<Collect> list = collectMapper.selectByExample(example);
        List<Spu> spu_list = new ArrayList<>();
        for (Collect col : list) {
            Long spu_id = col.getSpu_id();
            //调用feign获取spu数据
            Result<Spu> spu = spuFeign.findById(spu_id);
            spu_list.add(spu.getData());
        }
        return spu_list;
    }

    /**
     * Provinces构建查询对象
     * @param collect
     * @return
     */
    public Example createExample(Collect collect){
        Example example=new Example(Collect.class);
        Example.Criteria criteria = example.createCriteria();
        if(collect!=null){
            // 用户名
            if(!StringUtils.isEmpty(collect.getUsername())){
                criteria.andEqualTo("username",collect.getUsername());
            }
            // 商品id
            if(collect.getSpu_id() > 0){
                criteria.andEqualTo("spu_id",collect.getSpu_id());
            }
        }
        return example;
    }
}
